package com.frxuexi.reprot

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.lifecycle.LifecycleCoroutineScope
import com.frxuexi.reprot.entity.CommitReportPage
import com.frxuexi.reprot.entity.ReportEntity
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date


@Composable
fun MyReportPage(list: List<ReportEntity>,
    onClick: () -> Unit, onCommitClick: (String, String, String) -> Unit){
    //dialog
    var isShow by remember {
        mutableStateOf(false)
    }
    var isShow2 by remember {
        mutableStateOf(false)
    }

    var type by remember {
        mutableStateOf("")
    }



    Scaffold( floatingActionButton = {
        if(!isShow2){
            Icon(tint=  Color.Blue ,painter = painterResource(id = R.drawable.icon159), contentDescription = "",
                modifier = Modifier.clickable {
                    isShow=true
                } )
        }

    }) {


        if (isShow2){
            CommitReportPage(type,{
                isShow2=false
            }){type,content,plan->
                onCommitClick(type,content,plan)
                //点击提交汇报，切换页面
                isShow2=false
            }
        }else{
            Column {
                Row(modifier= Modifier.height(50.dp),verticalAlignment=Alignment.CenterVertically) {
                    TextButton(onClick = { onClick() }) {
                        Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "")
                    }
                    Text(
                        text = "工作汇报",
                        modifier = Modifier.fillMaxWidth(),
                        textAlign = TextAlign.Center
                    )
                }
                reprotItem(list, modifier = Modifier.padding(it))
            }
        }

    }

    if (isShow){
        Dialog(onDismissRequest = { isShow=false }) {
            Card(modifier = Modifier
                .width(600.dp)
                .padding(20.dp), shape = RoundedCornerShape(20.dp)) {
                Column(modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp)) {
                    Text(text = "新建汇报")
                    Spacer(modifier = Modifier.height(10.dp))
                    Row (modifier = Modifier.fillMaxWidth(),horizontalArrangement = Arrangement.SpaceAround){
                        DialogItem(title = "1", content = "日报"){
                            isShow=false
                            isShow2=true
                            type="日报"
                        }
                        DialogItem(title = "7", content = "周报"){
                            isShow=false
                            isShow2=true
                            type="周报"
                        }
                        DialogItem(title = "30", content = "月报"){
                            isShow=false
                            isShow2=true
                            type="月报"
                        }
                    }
                    Spacer(modifier = Modifier.height(10.dp))
                    Row (modifier = Modifier.fillMaxWidth(),horizontalArrangement = Arrangement.SpaceAround){
                        DialogItem(title = "季度", content = "季度报表"){
                            isShow=false
                            isShow2=true
                            type="hide"
                        }
                        DialogItem(title = "业绩", content = "业绩报表"){
                            isShow=false
                            isShow2=true
                            type="hide"
                        }
                        DialogItem(title = "营业", content = "营业财报"){
                            isShow=false
                            isShow2=true
                            type="hide"
                        }
                    }
                }
            }
        }
    }




}

@Composable
fun DialogItem(title:String,content:String, onClick: () -> Unit){

    Column(modifier = Modifier.clickable {
        onClick()
    }) {
        Column(horizontalAlignment=Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,modifier= Modifier
            .size(50.dp)
            .background(
                Color.LightGray,
                CircleShape
            )
            .padding(6.dp)
            .border(1.dp, Color.Blue, shape = RoundedCornerShape(6.dp))) {
            Text(text = title)
        }

        Text(text = content,modifier= Modifier.offset(x=20.dp))
    }





}
@Composable
fun reprotItem(list: List<ReportEntity>,modifier: Modifier){
    LazyColumn(content = {
        items(list){
            reprotsub(reportData = it)
        }
    })
}
@Composable
fun reprotsub(reportData: ReportEntity){
    Column(verticalArrangement=Arrangement.spacedBy(5.dp), modifier = Modifier.padding(start = 10.dp, top = 10.dp)) {
        Row(modifier = Modifier.height(40.dp),verticalAlignment= Alignment.CenterVertically,
            horizontalArrangement= Arrangement.spacedBy(10.dp)) {
            Image(modifier = Modifier
                .size(40.dp)
                .clip(CircleShape), painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "")

            Text(text = reportData.reportRemind)
        }
        Text(text = reportData.reportNowTitle,fontWeight= FontWeight.Bold)
        Text(text = reportData.reportNowValue,modifier = Modifier.padding(start = 20.dp))
        Spacer(modifier = Modifier.height(10.dp))
        Text(text = reportData.reportNextTitle,fontWeight= FontWeight.Bold)
        Text(text = reportData.reportNextValue,modifier = Modifier.padding(start = 20.dp))

        //时间处理
        val time = reportData.reportTime
        var sdf = SimpleDateFormat("2023-MM-dd")
        val mytime = sdf.format(time)
        Text(text = "${mytime}发布")
    }
}